<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <script type="text/javascript" src="http://mat1.gtimg.com/libs/jquery/1.12.0/jquery.js"></script>
</head>
<body>
<form action="" method="post">
    用户名: <input type="text" name="uname" onblur="n()" id="user"><span id="name"></span><br>
    密码: <input type="text" name="pwd"><br>
    <button>注册</button>
</form>
<script>
    function n() {
        var user = $('#user').val()
        if (user.length === 0) {
            $('#name').html('用户名不能为空')
            $('#name').css('color', 'red')
        } else {
            $.ajax({
                url: '{% url 'only' %}',
                type: 'GET',
                data: ({'user': user}),
                success:function(data) {
                    if (data.flag === 1) {
                        $('#name').html('此用户已存在')
                        $('#name').css('color', 'red')
                    } else {
                        $('#name').html('√')
                        $('#name').css('color', 'blue')
                    }
                }
            })
        }

    }
</script>
</body>
</html>